package org.gap.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.gap.domain.Relations;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Relations entities.
 * 
 */
@Repository("RelationsDAO")
@Transactional
public class RelationsDAOImpl extends AbstractJpaDao<Relations> implements
		RelationsDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Relations.class }));

	/**
	 * EntityManager injected by Spring for persistence unit 
	 *
	 */
	@PersistenceContext(unitName = "")
	private EntityManager entityManager;

	/**
	 * Instantiates a new RelationsDAOImpl
	 *
	 */
	public RelationsDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findRelationsByRelRelation
	 *
	 */
	@Transactional
	public Relations findRelationsByRelRelation(String relRelation) throws DataAccessException {

		return findRelationsByRelRelation(relRelation, -1, -1);
	}

	/**
	 * JPQL Query - findRelationsByRelRelation
	 *
	 */

	@Transactional
	public Relations findRelationsByRelRelation(String relRelation, int startResult, int maxRows) throws DataAccessException {
		try {
			return executeQueryByNameSingleResult("findRelationsByRelRelation", relRelation);
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findRelationsByRelLibelleAscContaining
	 *
	 */
	@Transactional
	public Set<Relations> findRelationsByRelLibelleAscContaining(String relLibelleAsc) throws DataAccessException {

		return findRelationsByRelLibelleAscContaining(relLibelleAsc, -1, -1);
	}

	/**
	 * JPQL Query - findRelationsByRelLibelleAscContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Relations> findRelationsByRelLibelleAscContaining(String relLibelleAsc, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findRelationsByRelLibelleAscContaining", startResult, maxRows, relLibelleAsc);
		return new LinkedHashSet<Relations>(query.getResultList());
	}

	/**
	 * JPQL Query - findRelationsByRelType
	 *
	 */
	@Transactional
	public Set<Relations> findRelationsByRelType(String relType) throws DataAccessException {

		return findRelationsByRelType(relType, -1, -1);
	}

	/**
	 * JPQL Query - findRelationsByRelType
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Relations> findRelationsByRelType(String relType, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findRelationsByRelType", startResult, maxRows, relType);
		return new LinkedHashSet<Relations>(query.getResultList());
	}

	/**
	 * JPQL Query - findRelationsByRelRelationContaining
	 *
	 */
	@Transactional
	public Set<Relations> findRelationsByRelRelationContaining(String relRelation) throws DataAccessException {

		return findRelationsByRelRelationContaining(relRelation, -1, -1);
	}

	/**
	 * JPQL Query - findRelationsByRelRelationContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Relations> findRelationsByRelRelationContaining(String relRelation, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findRelationsByRelRelationContaining", startResult, maxRows, relRelation);
		return new LinkedHashSet<Relations>(query.getResultList());
	}

	/**
	 * JPQL Query - findRelationsByRelTypeContaining
	 *
	 */
	@Transactional
	public Set<Relations> findRelationsByRelTypeContaining(String relType) throws DataAccessException {

		return findRelationsByRelTypeContaining(relType, -1, -1);
	}

	/**
	 * JPQL Query - findRelationsByRelTypeContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Relations> findRelationsByRelTypeContaining(String relType, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findRelationsByRelTypeContaining", startResult, maxRows, relType);
		return new LinkedHashSet<Relations>(query.getResultList());
	}

	/**
	 * JPQL Query - findRelationsByPrimaryKey
	 *
	 */
	@Transactional
	public Relations findRelationsByPrimaryKey(String relRelation) throws DataAccessException {

		return findRelationsByPrimaryKey(relRelation, -1, -1);
	}

	/**
	 * JPQL Query - findRelationsByPrimaryKey
	 *
	 */

	@Transactional
	public Relations findRelationsByPrimaryKey(String relRelation, int startResult, int maxRows) throws DataAccessException {
		try {
			return executeQueryByNameSingleResult("findRelationsByPrimaryKey", relRelation);
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findRelationsByRelLibelleDesc
	 *
	 */
	@Transactional
	public Set<Relations> findRelationsByRelLibelleDesc(String relLibelleDesc) throws DataAccessException {

		return findRelationsByRelLibelleDesc(relLibelleDesc, -1, -1);
	}

	/**
	 * JPQL Query - findRelationsByRelLibelleDesc
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Relations> findRelationsByRelLibelleDesc(String relLibelleDesc, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findRelationsByRelLibelleDesc", startResult, maxRows, relLibelleDesc);
		return new LinkedHashSet<Relations>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllRelationss
	 *
	 */
	@Transactional
	public Set<Relations> findAllRelationss() throws DataAccessException {

		return findAllRelationss(-1, -1);
	}

	/**
	 * JPQL Query - findAllRelationss
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Relations> findAllRelationss(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllRelationss", startResult, maxRows);
		return new LinkedHashSet<Relations>(query.getResultList());
	}

	/**
	 * JPQL Query - findRelationsByRelLibelleDescContaining
	 *
	 */
	@Transactional
	public Set<Relations> findRelationsByRelLibelleDescContaining(String relLibelleDesc) throws DataAccessException {

		return findRelationsByRelLibelleDescContaining(relLibelleDesc, -1, -1);
	}

	/**
	 * JPQL Query - findRelationsByRelLibelleDescContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Relations> findRelationsByRelLibelleDescContaining(String relLibelleDesc, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findRelationsByRelLibelleDescContaining", startResult, maxRows, relLibelleDesc);
		return new LinkedHashSet<Relations>(query.getResultList());
	}

	/**
	 * JPQL Query - findRelationsByRelLibelleAsc
	 *
	 */
	@Transactional
	public Set<Relations> findRelationsByRelLibelleAsc(String relLibelleAsc) throws DataAccessException {

		return findRelationsByRelLibelleAsc(relLibelleAsc, -1, -1);
	}

	/**
	 * JPQL Query - findRelationsByRelLibelleAsc
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Relations> findRelationsByRelLibelleAsc(String relLibelleAsc, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findRelationsByRelLibelleAsc", startResult, maxRows, relLibelleAsc);
		return new LinkedHashSet<Relations>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Relations entity) {
		return true;
	}
}
